import house from '@/pages/signContact/template/house.vue';
import mintPicker from './template/mintPicker.vue';
import { Toast,MessageBox } from 'mint-ui';
import {mobileTest,reLoginWX} from '@/util/util.js';
import comfirmLogin from '@/components/comfirm.vue';
import {mapState,mapMutations} from 'Vuex';

export default {
  name: "",
  data: () => ({
    houseInfo:'',
    houseHeaderImg:'',      //房源图片，获取图片列表的第一张
    name:'',
    sex:1,
    switchLeft:'left',
    reserveTime:'',   //预约时间
    reserveTimeShow:'',   //预约时间页面显示
    mobile:'',
    yzmCode:'',
    isMintPicker:false,
    comfirmTitle:{
      comfirmTop:'提示',
      comfirmCon:'验证码错误',
      comfirmBtn:['取消','确认'],
    },
    isWrong:false,   //显示确认框
    difMobile:false,    //手机号码输入与用户本身号码不一致 验证码开关，true为需要验证
    isGetYzmTime:false,   //倒计时内不能重复点击获取验证码
    yzmTime:60,
  }),
  components: {
    house,
    mintPicker,
    comfirmLogin,
  },
  mounted() {
    //do something after mounting vue instance
    let that = this;
    let houseId = that.$route.query.houseId;
    console.log(houseId);
    network.get(configUrl.HOUSEDETAIL_URL,{houseId:houseId},function(res){
      console.log(res);
      that.houseInfo = res.data.house;
      that.houseHeaderImg = res.data.housePicList[0];
    },function(err){
      console.log(err);
    });
    let userInfo_storage = JSON.parse(window.localStorage.getItem('userInfo'));
    that.name = that.userInfo.userName;
    that.mobile = that.userInfo.mobile? that.userInfo.mobile : userInfo_storage.mobile;
  },
  computed: {
    ...mapState({
      userInfo:state=>state.login.userInfo,
    })
  },
  methods: {
    // 设置性别
    switchSex() {
      let that = this;
      let sex = that.sex;
      if(sex==1){
        that.switchLeft = 'right';
        that.sex = 0;
      }else{
        that.switchLeft = 'left';
        that.sex = 1;
      }
    },
    // 显示日期
    openPicker() {
      let that = this;
      that.isMintPicker = true;
    },
    // 日期选择
    mintPickerBtn(data) {
      let that = this;
      console.log(data);
      that.reserveTimeShow = data.mintChooseValue[0].value+' '+data.mintChooseValue[1];
      console.log(that.reserveTimeShow);
      let year = data.mintChooseValue[0].year;
      let month = data.mintChooseValue[0].month;
      let day = data.mintChooseValue[0].day;
      let reserveTime = parseInt(data.mintChooseValue[1])+':00:00';
      that.reserveTime = year+'-'+month+'-'+day+' '+reserveTime;
      that.isMintPicker = false;
      // console.log(that.reserveTime);
    },
    iptMobile() {
      console.log(this.mobile);
      let that = this;
      let mobile = that.mobile;
      if(mobile.length==11 && (that.userInfo.mobile != '') ){
        console.log('号码输入结束');
        if(mobile != that.userInfo.mobile) {
          that.difMobile = true;
        }
      }
    },
    // 获取验证码
    getYzm() {
      let that = this;
      let mobile = that.mobile;
      if(mobileTest(mobile)){
        Toast('请输入正确的手机号');
        return false;
      }
      let yzmParam = {
        mobile:mobile,
        pinType:6     //验证码类型 1 注册登录 2在线委托 3合同签约 4手机号换绑 5修改智能门锁密码 6预约看房
      };
      network.get(configUrl.LOGINGETYZM_URL,yzmParam,function(res){
        console.log(res);
        Toast('验证码发送成功');
        that.yzmTime = 60;
        that.isGetYzmTime = true;

        let yzmInterval = setInterval(()=>{
          that.yzmTime --;
          if(that.yzmTime==0){
            that.isGetYzmTime = false;
            return false;
          }
        },1000);
      },function(err){
        console.log(err);
      });
    },
    // yzmCodeComfirm() {
    //   let yzmCode = this.yzmCode;
    //   if(yzmCode.length==6 && yzmCode!='666666'){
    //     this.isWrong = true;
    //   }
    // },
    comfirmEvent() {
      this.isWrong = false;
    },
    // 提交
    submitReservation() {
      let that = this;
      let houseId = that.houseInfo.houseId;
      let linkName = that.name;
      if(linkName==''){
        Toast('请输入姓名');
        return false;
      }
      let sex = that.sex;
      let reserveTime = that.reserveTime;
      if(reserveTime==''){
        Toast('请选择预约时间');
        return false;
      }
      let mobile = that.mobile;
      if(mobile==''){
        Toast('请输入手机号码');
        return false;
      }
      if(mobileTest(mobile)){
        Toast('请输入正确的手机号');
        return false;
      }
      let yzmCode = that.yzmCode;
      if(that.difMobile){
        if(yzmCode==''){
          Toast('请输入填写验证码');
          return false;
        }
      }
      let reservaParam = {
        houseId:houseId,
        linkName:linkName,
        sex:sex,
        reserveTime:reserveTime,
        mobile:mobile,
        yzmCode:that.yzmCode
      }
      network.post(configUrl.HOUSERESERVE_URL,reservaParam,function(res){
        console.log(res);
        if(res.code==401) {
          reLoginWX();
        }else{
          Toast('预约成功');
          setTimeout(()=>{
            that.$router.go(-1);
          },800);

        }
      },function(err){
        console.log(err);
      });
    }
  }
}
